package com.korustar.autumnal.codemodel;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;

import com.korustar.autumnal.XmlBeanInterpretation;
import com.sun.codemodel.JClassAlreadyExistsException;
/**
 * Entry point for a Spring->AutumnalContainer transformation 
 * 
 * @author amir
 *
 */
public class Main {

	private static final String LOG4J_CONFIGURATION = "log4j.configuration";
	private static final String SRC_TEST_OUT = "src/test/out/";

	public static void main(String[] args) throws IOException, JClassAlreadyExistsException {
		if(!System.getProperties().containsKey(LOG4J_CONFIGURATION))
        {
			try {
				Class.forName("org.apache.log4j.BasicConfigurator");
                System.out.println("Configuring log4j's BasicConfigurator. (No system property '"+LOG4J_CONFIGURATION+"')");
                org.apache.log4j.BasicConfigurator.configure();
			} catch(ClassNotFoundException e) {
				System.err.println("info: log4j not on classpath");
			}
        }
		if(args.length>0) {
			String filename = args[0];
			File file = new File(filename);
			
			BeanDefinitionRegistry registry = XmlBeanInterpretation.getBeanRegistry(file);
			CodeModelDefaultContainer container= new CodeModelDefaultContainer();
			CodeModelContainerModel model = container.startContainer().getBuilder().build(registry);
			String outputDir;
			if(args.length>1) {
				outputDir= args[1];
			} else {
				outputDir=SRC_TEST_OUT;
			}
			container.getOutputter().setFolderName(outputDir);
			container.getOutputter().go(model);
			/*
			new File(outputDir).mkdirs();
			
			os= new FileOutputStream(outputDir+model.getContainerClassName()+".java");
			model.outputToStream(os);
			*/
			
		} else {
			System.err.println("Enter filename as first program arg");
		}

	}
}
